package leetcode2;

import java.util.HashSet;
import java.util.Set;

public class Day4 {

    public static void main(String[] args) {
        String s = "pwwkew";

        lengthOfLongestSubstring(s);
    }


    //718.最长重复子数组
    public int findLength(int[] nums1, int[] nums2) {
        //子数组 -- 需要连续
        int m=nums1.length,n=nums2.length;
        int[][] dp = new int[m+1][n+1];
        int len=0;
        for(int i=1;i<=m;i++) {
            for(int j=1;j<=n;j++) {
                if(nums1[i-1] == nums2[j-1]) {
                    dp[i][j]=dp[i-1][j-1] + 1;
                }
                len=Math.max(len,dp[i][j]);
            }
        }
        return len;
    }



    //3.最长无重复子序列的长度
    public static int lengthOfLongestSubstring(String s) {
        int n=s.length();
        int len=0;
        Set<Character> set = new HashSet<>();
        for(int left=0,right=0;right<n;right++) {
            char ch = s.charAt(right);
            while (set.contains(ch)) {
                set.remove(s.charAt(left++));
            }
            set.add(ch);
            len = Math.max(len,right-left+1);
        }
        return len;
    }
}
